home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5622 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  82 lines

  1. Path: ix.netcom.com!netnews
  2. From: judgemi@ix.netcom.com(Michael Judge )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: problem writing class to a file.....  -  temp.txt [1/1]
  5. Date: 5 Feb 1996 23:19:34 GMT
  6. Organization: Netcom
  7. Message-ID: <4f63a6$kqu@cloner2.ix.netcom.com>
  8. References: <4f5a5e$3ca@geraldo.cc.utexas.edu>
  9. NNTP-Posting-Host: bos-ma9-23.ix.netcom.com
  10. X-NETCOM-Date: Mon Feb 05  3:19:34 PM PST 1996
  11.  
  12. In <4f5a5e$3ca@geraldo.cc.utexas.edu> wharris@mail.utexas.edu (W. 
  13. Harris) writes: 
  14. >
  15. >
  16. >   The problem I'm having is in the code "outfile.write(char
  17. *)&.....".
  18. >   The book I used to learn C, C++ indicates this should work.  The
  19. >   compiler error I'm getting is "sizeof cannot be used on a 
  20. >   function".  Code has been reduced; the struct definition and the 
  21. >   constructor have been left out.  I've also opened the file for
  22. binary
  23. >   output. (not indicated in code)
  24. >   
  25. >
  26. >   
  27. >  class CarData
  28. >    {
  29. >    private :
  30. >
  31. >    char VIN[7],Stock[7],Tag[7],Tech[4],Color[12],Model[16],Year[5];
  32. >    char EstAmt[6],Name[35],Address[32],City[19],State[3],Zip[6];
  33. >    char Area[4],Prefix[4],Num[5],WArea[4],WPrefix[4],WNum[5];
  34. >    char FnlAmt[6];
  35. >
  36. >    public :
  37. >
  38. >    CarData(struct astruct1 CarInf);
  39. >     };
  40. //*********************************************************************
  41. >
  42. >
  43. >void main()
  44. >    {
  45. >
  46. >       
  47. >    CarData CarConst1(struct astruct1 CarInfo);   //1st instance of
  48. cnstctr
  49. >    outfile.write((char *)&CarConst1, sizeof(CarConst1)); 
  50. //*****ERROR IS OCCURRING HERE*
  51. >    getch();
  52. >    closegraph();
  53. >    }
  54. >
  55. >  
  56. //*******************************************************************
  57. >
  58.  
  59. One way to get around this is by using sizeof(CarData) not 
  60. an object variable. Without the complex constructor the code you have
  61. will work fine.
  62.  
  63. this is also O.K. if you code like this.
  64.  
  65. void main()
  66. {
  67.     astruct1 CarInfo;  
  68.     CarData CarConst1(CarInfo);   //1st instance of cnstctr
  69.     outfile.write((char *)&CarConst1, sizeof(CarConst1));  IS OCCURRING
  70.     getch();
  71.     closegraph();
  72. }
  73.  
  74.  
  75. if you declare the variable outside of the constructor, it works fine.
  76. Why?? I have no idea! are you using Visual C++ v1.52? I am and got the
  77. same result. if this is correct then I would have failed the test.
  78.  
  79. MJ
  80.